/*
 This file 'File' is part of Firebird Integrated Solution 1.0

 Copyright (c) 2014 Lincong

 Contact:  
 Email: lincong1987@gmail.com

 QQ: 159257119

 See Usage at http://www.jplatformx.com/firebird

 Create date: 2014-06-19 17:01
 */

define(function (require, exports, module) {

	firebird.api.mix("system.fileext", {
		// key: [type, desc]
		zip: ["rar", "压缩文件"],
		rar: ["rar", "压缩文件"],
		tar: ["rar", "压缩文件"],
		war: ["rar", "压缩文件"],
		docx: ["word", "电子文档"],
		doc: ["word", "电子文档"],
		xls: ["excel", "电子表格"],
		xlsx: ["excel", "电子表格"],
		exe: ["exe", "应用程序"],
		bat: ["exe", "应用程序"],
		pdf: ["pdf", "电子文稿"],
		pptx: ["ppt", "演示文稿"],
		ppt: ["ppt", "演示文稿"],
		js: ["js", "JavaScript脚本"],
		json: ["js", "JavaScript脚本"],
		txt: ["txt", "文本文档"],
		html: ["txt", "网页"],
		jsp: ["txt", "网页"],
		php: ["txt", "网页"],
		asp: ["txt", "网页"],
		rb: ["txt", "网页"],
		sh: ["txt", "网页"],
		xml: ["txt", "网页"],
		jar: ["txt", "网页"],
		sql: ["txt", "网页"],
		log: ["txt", "网页"],
		css: ["css", "样式表"],
		less: ["css", "样式表"],
		sass: ["css", "样式表"],
		wma: ["music", "音乐"],
		mp3: ["music", "音乐"],
		amr: ["music", "音乐"],
		ogg: ["music", "音乐"],
		ape: ["music", "音乐"],
		flac: ["music", "音乐"],
		wav: ["music", "音乐"],
		jpg: ["image", "图片"],
		gif: ["image", "图片"],
		jpeg: ["image", "图片"],
		png: ["image", "图片"],
		flv: ["vedio", "视频"],
		mkv: ["vedio", "视频"],
		mp4: ["vedio", "视频"],
		mpeg: ["vedio", "视频"],
		mpg2: ["vedio", "视频"],
		rmvb: ["vedio", "视频"],
		avi: ["vedio", "视频"]
	});

	/**
	 * Firebird.lang.File
	 * @class Firebird.lang.File
	 */
	Firebird.define("Firebird.lang.File", {
		extend: "Firebird.Model",
		config: {
			id: null,
			filename: null,
			content: null,
			ext: null,
			path: null,
			lastModified: null,
			size: 0,
			md5: "",
			sha1: "",
			service: null,
			exist: null,
			onCkeckExistStart: null,
			onCkeckExistComplete: null,
			onCkeckExistSuccess: null,
			onCkeckExistError: null,
			onCreate: null
		},
		id: null,
		filename: "",
		content: null,
		ext: null,
		path: null,
		lastModified: null,
		size: 0,
		md5: "",
		sha1: "",
		service: {
			isExist: null,
			reader: function (file) {
				return file;
			}
		},
		exist: true,
		toString: function () {
			return this.getName() + this.getExt();
		},
		_init: function () {
			this.onCreate.apply(this, []);
		},
		onCreate: function () {
		},
		onCkeckExistStart: function () {
		},
		onCkeckExistComplete: function () {
		},
		onCkeckExistSuccess: function () {
		},
		onCkeckExistError: function () {
		},
		isExist: function () {
			var me = this;
			me.exist = false;
			me.onCkeckExistStart.apply(me, []);
			var xhr = $.ajax({
				url: me.path + me.name,
				type: "get",
				dataType: "text",
				complete: function () {
					me.onCkeckExistComplete.apply(me, arguments);
				},
				error: function () {
					me.onCkeckExistError.apply(me, arguments);
				},
				success: function () {
					me.exist = true;
					me.onCkeckExistSuccess.apply(me, arguments);
					xhr = null;
				}
			});
		},
		isExistByService: function () {
			var me = this;
			me.exist = false;
			me.onCkeckExistStart.apply(me, []);
			var xhr = $.ajax({
				url: me.service.isExist,
				type: "get",
				dataType: "text",
				data: me.reader.apply(me, [file]),
				complete: function () {
					me.onCkeckExistComplete.apply(me, arguments);
				},
				error: function () {
					me.onCkeckExistError.apply(me, arguments);
				},
				success: function () {
					me.exist = true;
					me.onCkeckExistSuccess.apply(me, arguments);
					xhr = null;
				}
			});
		},
		isImage: function () {
			var me = this,
				exts = firebird.api.get("system.fileext"),
				is = false;

			$.each(exts, function (v, k) {
				if (k == me.ext && v[0] == "image") {
					is = true;
					return;
				}
			});

			return is;
		}
	});

	module.exports = Firebird.lang.File;

});